﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using CustomControl;
using GalaSoft.MvvmLight.Messaging;
using ListViewDragDrop.ServiceProviders.UI;
using TeacherLeftHand.Messages;
using TeacherLeftHand.View;

namespace TeacherLeftHand.DocEditor
{
    /// <summary>
    /// Interaction logic for TeachingPlanDocument.xaml
    /// </summary>
    public partial class TeachingPlanDocument : UserControl
    {
        public static RoutedEvent FileChangedEvent = EventManager.RegisterRoutedEvent("FileChanged", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(TeachingPlanDocument));
        private bool _fileChanged;
        public RichTextEditor editor = new RichTextEditor();
        private KnowledgeItemFilterMessage kiFilter = new KnowledgeItemFilterMessage();
        private TeachingMethodFilterMessage tmFilter = new TeachingMethodFilterMessage();
        public TeachingPlanDocument()
        {
            InitializeComponent();
            this.AddHandler(ActivityView.ActivityChangedEvent, new RoutedEventHandler(ActivityChangedEvent_Handler));
            this.AddHandler(RichTextEditor.RichTextEditorTextChangedEvent, new RoutedEventHandler(RichTextEditorTextChangedEvent_Handler));
            var activities = new ObservableCollection<ActivityView>
                                 {
                                     new ActivityView(),
                                     new ActivityView(),
                                     new ActivityView()
                                 };
            this.listOfActivities.ItemsSource = activities;

            var dragdrop = new ListViewDragDropManager<ActivityView>(this.listOfActivities);

            //binding 
            //cmbTPTopic.ItemsSource = (new KnowledgeItemAccessor()).GetTopicList();
            //this.Loaded += new RoutedEventHandler(TeachingPlanDocument_Loaded);
        }

        //void TeachingPlanDocument_Loaded(object sender, RoutedEventArgs e)
        //{
        //    var itemSource = this.listOfActivities.ItemsSource as ObservableCollection<ActivityView>;
        //    if (itemSource != null)
        //    {
        //        itemSource.CollectionChanged += activities_CollectionChanged;
        //    }
        //}


        private void btnAddActivity_Click(object sender, RoutedEventArgs e)
        {
            IEditableCollectionView items = this.listOfActivities.Items; //Cast to interface
            if (items.CanAddNew)
            {
                items.AddNew();
                items.CommitNew();
            }
        }

        //private void btnDeleteActivity_Click(object sender, RoutedEventArgs e)
        //{
        //    IEditableCollectionView items = this.listOfActivities.Items; //Cast to interface
        //    if (items.CanRemove)
        //    {
        //        items.Remove(listOfActivities.SelectedItem);
        //        items.CommitNew();
        //    }
        //}
        public bool FileChanged
        {
            get
            {
                return _fileChanged;
            }
            set
            {
                if (value != _fileChanged)
                {
                    _fileChanged = value;
                    this.RaiseEvent(new RoutedEventArgs(FileChangedEvent));
                }
            }
        }
        // This function is used to raise MouseWheel event for ScrollViewer
        private void listOfActivities_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                eventArg.Source = sender;
                var parent = ((Control)sender).Parent as UIElement;
                if (parent != null)
                {
                    parent.RaiseEvent(eventArg);
                }
            }
        }

        private void SelectedClassLevelChanged(object sender, RoutedEventArgs e)
        {
            FileChanged = true;
            if (!cmbTPSubject.IsEditable)
            {
                cmbTPSubject.IsEnabled = true;
            }

            if (IsValid)
            {
                stackBody.IsEnabled = true;
            }

            if (kiFilter != null)
            {
                kiFilter.ClassLevel = (int)cmbTPClass.SelectedValue;
            }

            if (IsKILoaded)
            {
                Messenger.Default.Send(kiFilter);
            }

            if (tmFilter != null)
            {
                tmFilter.ClassLevel = (int)cmbTPClass.SelectedValue;
            }
            if (IsTMLoaded)
            {
                Messenger.Default.Send(tmFilter);
            }
        }

        private void TopicTextChanged(object sender, RoutedEventArgs e)
        {
            FileChanged = true;
            if (IsValid)
            {
                stackBody.IsEnabled = true;
            }
        }

        private void SelectedSubjectChanged(object sender, RoutedEventArgs e)
        {
            FileChanged = true;
            if (!cmbTPTopic.IsEnabled)
            {
                cmbTPTopic.IsEnabled = true;
                cmbTPTopicNumber.IsEnabled = true;
            }

            if (IsValid)
            {
                stackBody.IsEnabled = true;
            }

            if (kiFilter != null)
            {
                kiFilter.Subject = cmbTPSubject.SelectedValue.ToString();
            }

            if (IsKILoaded)
            {
                Messenger.Default.Send(kiFilter);
            }

            if (tmFilter != null)
            {
                tmFilter.Subject = cmbTPSubject.SelectedValue.ToString();
            }
            if (IsTMLoaded)
            {
                Messenger.Default.Send(tmFilter);
                tmFilter = null;
            }
        }

        private void SelectedTopicNoChanged(object sender, RoutedEventArgs e)
        {
            FileChanged = true;
            if (kiFilter != null)
            {kiFilter.TopicNo = cmbTPTopicNumber.Value ?? 0;}

            if (IsValid)
            {
                stackBody.IsEnabled = true;
            }

            if (IsKILoaded)
            {
                Messenger.Default.Send(kiFilter);
                kiFilter = null;
            }

        }

        private bool IsTMLoaded
        {
            get
            {
                if (tmFilter == null) return false;
                if (tmFilter.ClassLevel >= 0 && !string.IsNullOrEmpty(tmFilter.Subject)) return true;
                return false;
            }
        }

        private bool IsKILoaded
        {
            get
            {
                if (kiFilter == null) return false;
                if (kiFilter.ClassLevel <= 0) return false;
                if (string.IsNullOrEmpty(kiFilter.Subject)) return false;
                if (kiFilter.TopicNo <= 0) return false;
                return true;
            }
        }

        private bool IsValid
        {
            get
            {

                if (cmbTPClass.SelectedValue == null) return false;
                if (cmbTPSubject.SelectedValue == null) return false;
                if (string.IsNullOrEmpty(cmbTPTopic.Text)) return false;

                return true;
            }
        }

        private void dtpTPComposeDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            FileChanged = true;
        }

        private void dtpTPTeachingDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            FileChanged = true;
        }
        private void tbxTPWeek_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
           FileChanged = true;
        }

        private void tbxTPSessionNumber_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            FileChanged = true;
        }

        //private void richTextEditor_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    FileChanged = true;
        //}


        private void ActivityChangedEvent_Handler(object sender, RoutedEventArgs e)
        {
            FileChanged = true;
        }
        private void RichTextEditorTextChangedEvent_Handler(object sender, RoutedEventArgs e)
        {
            FileChanged = true;
        }
        //private void activities_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        //{
        //    FileChanged = true;
        //}

       
    }
}
